//
//  KDetailCollectionCell1.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "KDetailCollectionCell1.h"
#import "FitHeader.h"
#import "KShiDetaiTableCell.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import <MJRefresh.h>
#import "KMethodViewController.h"
@implementation KDetailCollectionCell1
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creatViews];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        _progressView = [[MBProgressHUD alloc]initWithView:self.contentView];
        _progressView.labelText = @"加载中...";
        [_progressView setMode:MBProgressHUDModeIndeterminate];
        [_progressView show:YES];
        [self.contentView addSubview:_progressView];
    }
    return self;
    //加载中提示图的初始化
    }
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KShiDetailCaiModel *model = [[KShiDetailCaiModel alloc]init];
    model = _dataSourceArray[indexPath.row];
    [self.delegate pushToMethod:model.dishes_id];
    //协议传值 传值到菜例具体方法页面中
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *identifier = @"cell";
    KShiDetaiTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[KShiDetaiTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.model = _dataSourceArray[indexPath.row];
    return cell;
}
-(void)setID:(NSString *)ID
{
    _ID = ID;
    _dataSourceArray = [NSMutableArray array];
    [self getData];
    _page = 1;
    
    //下拉刷新
    _tableView.header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [_dataSourceArray removeAllObjects];
        _page = 1;//刷新数据并回到最顶部
        [self getData];
    }];
    
    //上拉加载
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page += 1;
        [self getData];
    }];
    

}
-(void)getData
{
    NSString *postUrlString = @"http://api.izhangchu.com/";
    NSString *bodyString = [NSString stringWithFormat:@"material_id=%@&page=%ld&methodName=MaterialDishes&size=10&version=4.02", _ID,_page];
    [HTTPMethod getDataByString:postUrlString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *allDataDic = [data objectForKey:@"data"];
        NSArray *dataArray = [allDataDic objectForKey:@"data"];
        NSDictionary *foodDic = [NSDictionary dictionary];
        for (foodDic in dataArray) {
            KShiDetailCaiModel *model = [[KShiDetailCaiModel alloc]init];
            [model setValuesForKeysWithDictionary:foodDic];
            [_dataSourceArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
        [_progressView removeFromSuperview];
       
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _tableView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
}
-(void)creatViews
{
    _tableView = [[UITableView alloc]init];
    [self.contentView addSubview:_tableView];
}

@end
